/*
   Project AwesOmeScript
   Copyright 2010 Domenico Gentner

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "stdafx.hpp"
#include "HtmlEditSettings.hpp"

const wxString ASS_STYLE_PATH = "/styles/";


HtmlEditSettings::HtmlEditSettings()
{
	ReadSettings();
}

HtmlEditSettings::~HtmlEditSettings()
{
	WriteSettings();
	MakeEmpty();
}

void HtmlEditSettings::ReadSettings()
{
	wxConfig* cfg = new wxConfig();	

	// First delete all styles
	MakeEmpty();	

	// Check if styles exist
	if ( cfg->HasGroup(ASS_STYLE_PATH) )
	{
		// Set Path
		cfg->SetPath(ASS_STYLE_PATH);

		{
			// Get wxSTC_H_DEFAULT
			wxString result = cfg->Read("wxSTC_H_DEFAULT");
			StyleDescriptor* d = GetStyleFromString(result);
			m_styles[wxSTC_H_DEFAULT] = d;
		}

		{
			// Get wxSTC_H_TAG
			wxString result = cfg->Read("wxSTC_H_TAG");
			StyleDescriptor* d = GetStyleFromString(result);
			m_styles[wxSTC_H_TAG] = d;
		}

		{
			// Get wxSTC_H_ATTRIBUTE
			wxString result = cfg->Read("wxSTC_H_ATTRIBUTE");
			StyleDescriptor* d = GetStyleFromString(result);
			m_styles[wxSTC_H_ATTRIBUTE] = d;
		}

		{
			// Get wxSTC_H_SINGLESTRING
			wxString result = cfg->Read("wxSTC_H_SINGLESTRING");
			StyleDescriptor* d = GetStyleFromString(result);
			m_styles[wxSTC_H_SINGLESTRING] = d;
		}

		{
			// Get wxSTC_H_NUMBER
			wxString result = cfg->Read("wxSTC_H_NUMBER");
			StyleDescriptor* d = GetStyleFromString(result);
			m_styles[wxSTC_H_NUMBER] = d;
		}

	}
	else
	{
		ResetToStandard();
	}
	delete cfg;
}

void HtmlEditSettings::WriteSettings()
{
	wxConfig* cfg = new wxConfig();
	cfg->SetPath(ASS_STYLE_PATH);
	cfg->Write("wxSTC_H_DEFAULT", this->MakeStyleString(m_styles[wxSTC_H_DEFAULT]));
	cfg->Write("wxSTC_H_TAG", this->MakeStyleString(m_styles[wxSTC_H_TAG]));
	cfg->Write("wxSTC_H_ATTRIBUTE", this->MakeStyleString(m_styles[wxSTC_H_ATTRIBUTE]));
	cfg->Write("wxSTC_H_SINGLESTRING", this->MakeStyleString(m_styles[wxSTC_H_SINGLESTRING]));
	cfg->Write("wxSTC_H_NUMBER", this->MakeStyleString(m_styles[wxSTC_H_NUMBER]));
	delete cfg;
}

void HtmlEditSettings::ResetToStandard()
{
	MakeEmpty();

	// Standard Styles
	m_styles.insert(TStylePair(wxSTC_H_DEFAULT, new StyleDescriptor(wxSTC_H_DEFAULT, _("Default Colors:"), wxColour(*wxBLACK), wxColour(*wxWHITE))));
	m_styles.insert(TStylePair(wxSTC_H_TAG, new StyleDescriptor(wxSTC_H_TAG, _("Color of tags (example: <font>):"), wxColour(*wxBLUE), wxColour(*wxWHITE))));
	m_styles.insert(TStylePair(wxSTC_H_ATTRIBUTE, new StyleDescriptor(wxSTC_H_ATTRIBUTE, _("Color of attributes (example: <font color>):"), wxColour("#008000"), wxColour(*wxWHITE))));	
	m_styles.insert(TStylePair(wxSTC_H_SINGLESTRING, new StyleDescriptor(wxSTC_H_SINGLESTRING, _("Color of strings (example: 'foobar'):"), wxColour("#FF8000"), wxColour(*wxWHITE))));
	m_styles.insert(TStylePair(wxSTC_H_NUMBER, new StyleDescriptor(wxSTC_H_NUMBER, _("Color of numbers:"), wxColour("#004080"), wxColour(*wxWHITE))));
}

void HtmlEditSettings::MakeEmpty()
{	
	TStyleList::iterator it = m_styles.begin();
	for ( it; it != m_styles.end(); ++it )
	{
		StyleDescriptor* d = it->second;
		delete d;
	}
	m_styles.erase(m_styles.begin(), m_styles.end());
}

StyleDescriptor* HtmlEditSettings::GetStyleFromString( const wxString& s )
{
	wxStringTokenizer tokens(s, "|");
	wxString s_id = tokens.GetNextToken();
	wxString description = tokens.GetNextToken();
	wxString s_foreground = tokens.GetNextToken();
	wxString s_background = tokens.GetNextToken();

	if ( s_id.IsEmpty() || description.IsEmpty() || s_foreground.IsEmpty() || s_background.IsEmpty() )
	{
		// TODO: Implement Error Handling
		return NULL;
	}
	else
	{
		// Get ID
		int id = 0;
		std::stringstream ss;
		ss << s_id.mb_str();
		ss >> id;

		// Get Colors
		wxColour foreground = wxColour(s_foreground);
		wxColour background = wxColour(s_background);

		StyleDescriptor* d = new StyleDescriptor(id, description, foreground, background);
		return d;
	}
	return NULL;
}

wxString HtmlEditSettings::MakeStyleString( StyleDescriptor* d )
{
	wxString s_id;
	s_id << d->GetId();
	wxString description = d->GetDescription();
	wxString s_foreground = d->GetForeground().GetAsString(wxC2S_HTML_SYNTAX);
	wxString s_background = d->GetBackground().GetAsString(wxC2S_HTML_SYNTAX);

	wxString result;
	result = s_id + "|" + description + "|" + s_foreground + "|" + s_background;
	return result;	
}

void HtmlEditSettings::ChangeStyle( int id, StyleDescriptor* d )
{
	StyleDescriptor* old = m_styles[id];
	delete old;
	m_styles[id] = d;
}